<?php
/**
 * 增强类RMP专项模型
 * @author jianhui
 *
 */
class specialProjectModel extends MY_Model{
    function specialProjectModel(){
        parent::MY_Model();
        $this->table = 'SpecialProject';
    }
    /**
     * 更新父类金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->CI->load->model('specialProcessModel');
        $this->CI->specialProcessModel->updateMoney($parentId);
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['procId']) && $data['procId'] ? (int)$data['procId'] : 0;
    }
    /**
     * 数据包装
     * @param array $list
     */
    function packing($list = array()){
        //标准动作ID集合
        $actionIds = array();
        foreach ($list as $v){
            in_array($v['actionType'], $actionIds) || $actionIds[] = $v['actionType'];
        }
        //标准动作列表
        $this->CI->load->model('actionModel');
        $actionList = $this->CI->actionModel->getCacheData('actionList');
        //基金类型
        $fundTypeList = $this->config->item('specialProjectFundType');
        //活动类型
        $eventType = $this->config->item('projectEventType');
        //活动分类
        $eventClass = $this->config->item('projectEventClass');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            //标准动作
            $v['actionTypeStr'] = isset($actionList[$v['actionType']]) ? $actionList[$v['actionType']]['name'] : '';
            if ($v['actionTypeStr'] == '其它'){
                $v['actionTypeStr'] .= '-'.$v['actionAlias'];
            }
            //基金类型
            $v['fundTypeStr'] = isset($fundTypeList[(int)$v['fundType']]) ? $fundTypeList[(int)$v['fundType']] : '';
            //活动类型
            $v['eventTypeStr'] = isset($eventType[$v['eventType']]) ? $eventType[$v['eventType']] : '';
            //活动分类
            $v['eventClassStr'] = isset($eventClass[$v['eventType']][$v['eventClass']]) ? $eventClass[$v['eventType']][$v['eventClass']] : '';
            if ($v['eventClass'] == -1){
                $v['eventClassStr'] .= '-'.$v['eventClassAlias'];
            }
            //预算审核状态
            $v['budgetAuditStr'] = isset($roleList[$v['budgetAuditRole']]) ? $roleList[$v['budgetAuditRole']].'已审核' : '';
            if ($v['budgetAuditStr']){
                $v['budgetAuditStr'] .= isset($auditRs[$v['budgetAuditRs']]) ? ','.$auditRs[$v['budgetAuditRs']] : '';
            }else{
                $v['budgetAuditStr'] = isset($auditRs[$v['budgetAuditRs']]) ? $auditRs[$v['budgetAuditRs']] : '未审核';
            }
            //结算审核状态
            $v['settleAuditStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            if ($v['settleAuditStr']){
                $v['settleAuditStr'] .= isset($auditRs[$v['settleAuditRs']]) ? ','.$auditRs[$v['settleAuditRs']] : '';
            }else{
                $v['settleAuditStr'] = isset($auditRs[$v['settleAuditRs']]) ? $auditRs[$v['settleAuditRs']] : '未审核';
            }
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 获取子类金额
     * @param int $projectId
     * @return multitype:number
     */
    function getDetailMoney($projectId){
        //广宣统计金额
        $this->CI->load->model('specialPGxModel');
        $gxMoney = $this->CI->specialPGxModel->getMoney($projectId);
        //线下活动金额
        $this->CI->load->model('specialPOfflineModel');
        $offlineMoney = $this->CI->specialPOfflineModel->getMoney($projectId);
        $data['gx'] = array(
            'budgetApply' => $gxMoney['budgetApply'],
            'budgetAudit' => $gxMoney['budgetAudit'],
            'settleApply' => $gxMoney['settleApply'],
            'settleAudit' => $gxMoney['settleAudit'],
        );
        $data['offline'] = array(
            'budgetApply' => $offlineMoney['budgetApply'],
            'budgetAudit' => $offlineMoney['budgetAudit'],
            'settleApply' => $offlineMoney['settleApply'],
            'settleAudit' => $offlineMoney['settleAudit'],
        );
        $data['total'] = array(
            'budgetApply' => $gxMoney['budgetApply'] + $offlineMoney['budgetApply'],
            'budgetAudit' => $gxMoney['budgetAudit'] + $offlineMoney['budgetAudit'],
            'settleApply' => $gxMoney['settleApply'] + $offlineMoney['settleApply'],
            'settleAudit' => $gxMoney['settleAudit'] + $offlineMoney['settleAudit'],
        );
        return $data;
    }
    /**
     * 更新项目金额
     * @param int $projectId
     */
    function updateMoney($projectId){
        $childMoney = $this->getDetailMoney($projectId);
        $findData = $this->getData($projectId);
        //主线流程数据
        $this->CI->load->model('specialProcessModel');
        $procData = $this->CI->specialProcessModel->getData($findData['procId']);
        $data = array(
            'budgetApply' => $childMoney['total']['budgetApply'],
            'budgetAudit' => $childMoney['total']['budgetAudit'],
            'settleApply' => $childMoney['total']['settleApply'],
            'settleAudit' => $childMoney['total']['settleAudit'],
        );
        $this->edit($projectId, $data);
    }
    /**
     * 统计对应父级ID的金额
     * @param int $procId
     * @return array
     */
    function getMoney($procId){
        $data = array(
            'budgetApply' => 0,//预算申请
            'budgetAudit' => 0,//预算审核
            'settleApply' => 0,//结算申请
            'settleAudit' => 0,//结算审核
            'buBudgetApply' => 0,//补报预算申请
            'buBudgetAudit' => 0,//补报预算审核
            'buSettleApply' => 0,//补报结算申请
            'buSettleAudit' => 0,//补报结算审核
        );
        $findList = $this->getList(array('procId' => $procId));
        foreach ($findList as $v){
            if ($v['isBubao'] == 0){
                $data['budgetApply'] += $v['budgetApply'];
                $data['budgetAudit'] += $v['budgetAudit'];
                $data['settleApply'] += $v['settleApply'];
                $data['settleAudit'] += $v['settleAudit'];
            }else{
                $data['buBudgetApply'] += $v['budgetApply'];
                $data['buBudgetAudit'] += $v['budgetAudit'];
                $data['buSettleApply'] += $v['settleApply'];
                $data['buSettleAudit'] += $v['settleAudit'];
            }
        }
        return $data;
    }
    /**
     * 获取结算各状态数量(结算页需要用到，主要用作统计)
     * @param int $singleId
     * @param number $isBubao
     * @return multitype:number
     */
    function getSettleStatusCount($procId, $isBubao=0){
        $count = array(
            -1  => 0,//未提交
            0   => 0,//未审核
            1   => 0,//已通过
            2   => 0,//不通过
            3   => 0,//返回修改
            4   => 0,//部分通过
        );
        $findList = $this->getList(array('procId'=>$procId, 'isBubao'=>$isBubao, 'budgetAuditRs' => array('in',array(1,4))));
        foreach ($findList as $v){
            if (isset($count[$v['settleAuditRs']])){
                $count[$v['settleAuditRs']] ++;
            }
        }
        return $count;
    }
    /**
     * 项目预算一键审核
     * @param int $id
     * @param number $audit
     * @param array $data
     * @return boolean
     */
    function budgetOneKeyAudit($projectId, $audit = 1, $user = array()){
        $this->CI->load->model('specialPGxModel');
        $this->CI->load->model('specialPOfflineModel');
        $this->CI->specialPGxModel->budgetAuditByProjectId($projectId, $audit,$user);
        $this->CI->specialPOfflineModel->budgetAuditByProjectId($projectId, $audit, $user);
        $this->edit($projectId, array('budgetAuditRole'=>$user['userRole']));
        $this->budgetUpdateStatus($projectId);
        //更新金额
        $this->updateMoney($projectId);
        return true;
    }
    /**
     * 更新预算审核状态
     * @param number $id 项目ID
     * @return object
     */
    function budgetUpdateStatus($projectId){
        return $this->edit($projectId, array('budgetAuditRs'=>$this->getBudgetStatus($projectId)));
    }
    /**
     * 获取项目预算审核状态值（预算审核统计，获取预算审核结果）
     * @param int $projectId
     * @return number
     */
    function getBudgetStatus($projectId){
        $this->CI->load->model('specialPGxModel');
        $this->CI->load->model('specialPOfflineModel');
        $gxStatusCount = $this->CI->specialPGxModel->getBudgetStatusCount($projectId);
        $offlineStatusCount = $this->CI->specialPOfflineModel->getBudgetStatusCount($projectId);
        $count = array(
            0 => $gxStatusCount[0]+$offlineStatusCount[0],//未审核数
            1 => $gxStatusCount[1]+$offlineStatusCount[1],//通过数
            2 => $gxStatusCount[2]+$offlineStatusCount[2],//不通过数
        );
        $sumCount = array_sum($count);
        if($sumCount > 0){
            if ($count[0] == $sumCount){
                return 0;//未审核
            }elseif ($count[1] == $sumCount){
                return 1;//通过
            }elseif ($count[2] == $sumCount){
                return 2;//不通过
            }else{
                return 4;//部分通过
            }
        }else{
            return 0;
        }
    }
    /**
     * 项目预算一键审核
     * @param int $id
     * @param number $audit
     * @param array $data
     * @return boolean
     */
    function settleOneKeyAudit($projectId, $audit = 1, $user = array()){
        $this->CI->load->model('specialPGxModel');
        $this->CI->load->model('specialPOfflineModel');
        $this->CI->specialPGxModel->settleAuditByProjectId($projectId, $audit, $user);
        $this->CI->specialPOfflineModel->settleAuditByProjectId($projectId, $audit, $user);
        $this->edit($projectId, array('settleAuditRole'=>$user['userRole']));
        $this->settleUpdateStatus($projectId);
        //更新金额
        $this->updateMoney($projectId);
        return true;
    }
    /**
     * 更新结算审核状态（结算审核统计，获取结算审核结果）
     * @param number $id 项目ID
     * @return object
     */
    function settleUpdateStatus($projectId){
        $data['settleAuditRs'] = $this->getSettleStatus($projectId);
        $data['settleAuditRs'] == 0 && $data['settleAuditRole'] = 0;
        return $this->edit($projectId, $data);
    }
    /**
     * 获取项目结算审核状态值
     * @param int $id
     */
    function getSettleStatus($projectId){
        $this->CI->load->model('specialPGxModel');
        $this->CI->load->model('specialPOfflineModel');
        $gxStatusCount = $this->CI->specialPGxModel->getSettleStatusCount($projectId);
        $offlineStatusCount = $this->CI->specialPOfflineModel->getSettleStatusCount($projectId);
        $count = array(
            0 => $gxStatusCount[0]+$offlineStatusCount[0],//未审核数
            1 => $gxStatusCount[1]+$offlineStatusCount[1],//通过数
            2 => $gxStatusCount[2]+$offlineStatusCount[2],//不通过数
            3 => $gxStatusCount[3]+$offlineStatusCount[3],//返回修改
        );
        $sumCount = array_sum($count);
        if($sumCount > 0){
            if ($count[0] == $sumCount){
                return 0;//未审核
            }elseif ($count[1] == $sumCount){
                return 1;//通过
            }elseif ($count[2] == $sumCount){
                return 2;//不通过
            }elseif ($count[3] == $sumCount){
                return 3;//返回修改
            }else{
                return 4;//部分通过
            }
        }else{
            return 0;
        }
    }
    /**
     * 获取项目费用列表
     * @param array $singleInfoList
     */
    function getMoneyDetailList($rmpList = array()){
        $initData = array(
            'budgetApply' => array(
                'online'     => 0,
                'offline'  => 0,
                'total'     => 0,
            ),
            'budgetAudit' => array(
                'online'     => 0,
                'offline'  => 0,
                'total'     => 0,
            ),
            'settleApply' => array(
                'online'     => 0,
                'offline'  => 0,
                'total'     => 0,
            ),
            'settleAudit' => array(
                'online'     => 0,
                'offline'  => 0,
                'total'     => 0,
            ),
        );
        $returnList = array();
        //初始化
        foreach ($rmpList as $k => $v){
            $returnList[$k] = $initData;
        }
        //广宣列表
        $this->CI->load->model('specialPGxModel');
        $gxWhere = array('projectId'=>array('in', array_keys($rmpList)));
        $gxList = $this->CI->specialPGxModel->getList($gxWhere, NULL, NULL, '', '`projectId` asc');
        //分析数据
        foreach ($gxList as $v){
            //预算申请
            $returnList[(int)$v['projectId']]['budgetApply']['online'] += $v['budgetApply'];
            $returnList[(int)$v['projectId']]['budgetApply']['total'] += $v['budgetApply'];
            //预算审核
            $returnList[(int)$v['projectId']]['budgetAudit']['online'] += $v['budgetAudit'];
            $returnList[(int)$v['projectId']]['budgetAudit']['total'] += $v['budgetAudit'];
            if($v['isSettle'] == 1){
                //结算申请
                $returnList[(int)$v['projectId']]['settleApply']['online'] += $v['settleApply'];
                $returnList[(int)$v['projectId']]['settleApply']['total'] += $v['settleApply'];
                //结算审核
                $returnList[(int)$v['projectId']]['settleAudit']['online'] += $v['settleAudit'];
                $returnList[(int)$v['projectId']]['settleAudit']['total'] += $v['settleAudit'];
            }
        }
    
        //线下列表
        $this->CI->load->model('specialPOfflineModel');
        $offlineWhere = array('projectId'=>array('in', array_keys($rmpList)));
        $offlineList = $this->CI->specialPOfflineModel->getList($offlineWhere, NULL, NULL, '', '`projectId` asc');
        //分析数据
        foreach ($offlineList as $v){
            //预算申请
            $returnList[(int)$v['projectId']]['budgetApply']['offline'] += $v['budgetApply'];
            $returnList[(int)$v['projectId']]['budgetApply']['total'] += $v['budgetApply'];
            //预算审核
            $returnList[(int)$v['projectId']]['budgetAudit']['offline'] += $v['budgetAudit'];
            $returnList[(int)$v['projectId']]['budgetAudit']['total'] += $v['budgetAudit'];
            if($v['isSettle'] == 1){
                //结算申请
                $returnList[(int)$v['projectId']]['settleApply']['offline'] += $v['settleApply'];
                $returnList[(int)$v['projectId']]['settleApply']['total'] += $v['settleApply'];
                //结算审核
                $returnList[(int)$v['projectId']]['settleAudit']['offline'] += $v['settleAudit'];
                $returnList[(int)$v['projectId']]['settleAudit']['total'] += $v['settleAudit'];
            }
        }
        return $returnList;
    }
    /**
     * 根据提供的明细列表和标准动作列表，分析数据，并以单店ID为键，标准动作字母组合为二级键返回数据（主要配合报表使用）
     * @param array $singleInfoList 明细列表
     * @param array $standardList
     */
    function explanAction($rmpList = array(), $actionList = array()){
        //初始化数据
        $initData = array();
        foreach ($actionList as $k => $v){
            $initData[$k] = array(
                'num' => 0,
                'budgetApply' => 0,
                'budgetAudit' => 0,
                'settleApply' => 0,
                'settleAudit' => 0
            );
        }
        //分析单店基金项目列表数据
        $returnList = array();
        foreach((array)$rmpList as $v){
            $procId = (int)$v['procId'];
            isset($returnList[$procId]) || $returnList[$procId] = $initData;
    
            //执行次数
            $returnList[$procId][$v['actionType']]['num'] ++;
            //预算申请费用
            $returnList[$procId][$v['actionType']]['budgetApply'] += $v['budgetApply'];
            //预算审核费用
            $returnList[$procId][$v['actionType']]['budgetAudit'] += $v['budgetAudit'];
            //结算申请费用
            $returnList[$procId][$v['actionType']]['settleApply'] += $v['settleApply'];
            //结算审核费用
            $returnList[$procId][$v['actionType']]['settleAudit'] += $v['settleAudit'];
        }
        return $returnList;
    }
}